package cn.niit.letuseatdemo.controller.admin;

import cn.niit.letuseatdemo.entity.domain.Store;
import cn.niit.letuseatdemo.service.StoreService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/*
  * @Description:处理【商家入驻审核】的控制器
  * @Author: zhang_bowen
  * @CreateTime: 2022/11/15  14:51 Unclosed character class near index 10
[\ [|\]|\s]
          ^
 * @return: null
  **/
@Slf4j
@RequestMapping("/admin/store")
@Controller
public class AdminStoreRegisterController {

    @Autowired
    private StoreService storeService;

    /*
     * @Description: 审核商家入驻（列表）页面的初始化及查询
     * @Author: zhang_bowen
     * @CreateTime: 2022/12/1  11:08
     * @return: java.lang.String
     **/
    @GetMapping("")
    public String index(Model model, Store store) {
        log.info("商家入驻列表查询的搜索条件是：" + store);
        /** 将查询条件写入 model 中，解决前端查询后条件丢失的情况 */
        model.addAttribute("store", store);
        //执行商家查询
        List<Store> storeAdminList = storeService.selectAdminStoreList(store);

        /*开启分页查询
          * @Description: TODO
          * @Author: zhang_bowen
          * @CreateTime: 2022/11/15  14:52 Unclosed character class near index 10
        [\ [|\]|\s]
                  ^
         * @return: java.lang.String
          **/
        System.out.println(storeAdminList);
        PageInfo<Store> pageInfo = new PageInfo<>(storeAdminList);
        log.info("分页对象 = {}", pageInfo);
        log.info("查询出来的商家入驻申请列表：{}", storeAdminList);
        model.addAttribute("storeAdminList", storeAdminList);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/store/list";
    }

    /*
      * @Description: 初始化审核页面
      * @Author: zhang_bowen
      * @CreateTime: 2022/12/3  15:39
 * @return: java.lang.String
      **/
    @GetMapping("/{storeId}")
    public String saveAdminIndex(Model model,@PathVariable("storeId")Long storeId){
        log.info("要修改的Id = " +storeId);
        Store store = null;
        if(null != storeId && storeId > 0){
            store = storeService.selectAdminStoreByStoreId(storeId);
        }else {
            store = new Store();
        }
        model.addAttribute("store",store);
        return "/admin/store/add";
    }

    /*
      * @Description: 根据ID修改审核状态（通过）
      * @Author: zhang_bowen
      * @CreateTime: 2022/12/3  18:02
 * @return: null
      **/
   @GetMapping("/auditSuccess/{id}")
    public String ediAdminSuccessSave(@PathVariable("id") Integer id){

        int rows = storeService.auditSuccess(id);
        if(rows <= 0){//修改失败
            return "/admin/store/add";
        }
        return "redirect:/admin/store";
    }

    /*
      * @Description: 根据ID修改审核状态（不通过）
      * @Author: zhang_bowen
      * @CreateTime: 2022/12/8  17:08
 * @return: null
      **/
    @GetMapping("/auditFail/{id}")
    public String ediAdminFailSave(@PathVariable("id") Integer id){

        int rows = storeService.auditFail(id);
        if(rows <= 0){//修改失败
            return "/admin/store/add";
        }
        return "redirect:/admin/store";
    }
}

